<html lang="en">
<head>
    <meta charset="utf-8"/>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="/js/my.js"></script>
    <title>博客详情</title>
</head>

<body>
    <span style="display: none;"><input id="aid" value="{{.id}}"></input></span>
    <div id="view" align="center" style="display:block;width:50%;margin:auto;">
        <span id="title" style="width:100%;">{{.title}}</span><br>
        <hr><br>
        <div id="content" style="white-space: pre-wrap;">{{.content}}</div>
        <br>更新于{{.update_time}}
        <br><button id="edit_bnt" onclick="edit();" style="display: none;">编辑</button>
    </div>
    <div id="update" align="center" style="display: none;width: 50%;margin: auto;">
        <input type="text" id="edit_title" style="width: 100%;" name="edit_title" value="{{.title}}"></input><br>
        <hr><br>
        <textarea id="edit_content" name="edit_content" rows="15" style="width: 100%;">{{.content}}</textarea><br><br>
        <button id="update_bnt" onclick="update();">提交</button>
        <span id="msg" style="color: red;"></span>
    </div>
    <script>
        function edit() {
            document.querySelector("#view").style.display='none';
            document.querySelector("#update").style.display='block';
        }
        function update() {
            var title = document.querySelector("#edit_title").value;
            var content = document.querySelector("#edit_content").value;
            var aid = document.querySelector("#aid").value;
            $.ajax({
                type:"POST",
                url:"/blog/update",
                data:{"title":title, "content": content, "aid":aid},
                beforeSend: function (request) {
                    var auth_token = get_auth_token();
                    request.setRequestHeader("auth_token", auth_token)
                },
                success:function (result) {
                    window.location.replace("/blog/"+aid);
                },
            }).fail(function(result, result1, result2) {
                $('#msg').html(result.responseText);
            });
        };
        $(document).ready(function(){
            var aid = document.querySelector("#aid").value;
            var auth_token = get_auth_token();
            $.ajax({
                type: "GET",
                url: "/blog/belong",
                data: {"aid": aid, "token": auth_token},
                success: function (result) {
                    if ("true"==result) {
                        document.querySelector("#edit_bnt").style.display='block';
                    }
                }
            }).fail(function (result, result1, result2){

            });
        });
    </script>
</body>